package com.orange.controller;

import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthMoneyResourceDataService;
import com.orange.handler.AuthResourceDataService;
import com.orange.handler.AuthSchoolRunningDateService;
import com.orange.handler.AuthStudentDataService;
import com.orange.handler.AuthTeachResourceService;
import com.orange.handler.AuthTeacherDataService;
import com.orange.modules.bean.BaseDataStatus;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.school_data.AuthMoneyResource;
import com.orange.modules.entity.school_data.AuthResourceData;
import com.orange.modules.entity.school_data.AuthSchoolRunningData;
import com.orange.modules.entity.school_data.AuthStudentData;
import com.orange.modules.entity.school_data.AuthTeachResource;
import com.orange.modules.entity.school_data.AuthTeacherData;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/baseData")
public class BaseDataController extends BaseController{
	
	private static final Logger log = Logger.getLogger(BaseDataController.class);
	
	@Autowired
	private AuthSchoolRunningDateService authSchoolRunningDateService;
	
	@Autowired
	private AuthStudentDataService authStudentDataService;
	
	@Autowired
	private AuthTeacherDataService authTeacherDataService;
	
	@Autowired
	private AuthResourceDataService authResourceDataService;
	
	@Autowired
	private AuthTeachResourceService authTeachResourceService;
	
	@Autowired
	private AuthMoneyResourceDataService authMoneyResourceDataService;
	
	/**
	 * 获取当前用户所有基本数据的填报状态
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAllBaseData" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAllBaseData(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try {
			BaseDataStatus bds = new BaseDataStatus();
			String pid=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==pid){
				return toResponse(bds,ReturnCode.NOLOGIN,"未登陆");
			}
			AuthSchoolRunningData asrd = authSchoolRunningDateService.queryAuthSchoolRunningDataBySchoolId(pid, null);//办学数据
			AuthStudentData asd = authStudentDataService.queryStudentDataBySchoolId(pid, null);
			AuthTeacherData atd = authTeacherDataService.queryAuthTeacherDataBySchoolId(pid, null);
			AuthResourceData ard = authResourceDataService.queryAuthResourceDataBySchoolId(pid, null);
			AuthTeachResource atr = authTeachResourceService.queryAuthTeachResourceDataBySchoolId(pid, null);
			AuthMoneyResource amr = authMoneyResourceDataService.queryAuthMoneyResourcesBySchoolId(pid, null);
			if(asrd!=null)
				bds.setRunningData(asrd.getStatus());
			if(asd!=null)
				bds.setStudentData(asd.getStatus());
			if(atd!=null)
				bds.setTeacherData(atd.getStatus());
			if(ard!=null)
				bds.setResourceData(ard.getStatus());
			if(atr!=null)
				bds.setTeachResourceData(atr.getStatus());
			if(amr!=null)
				bds.setMomeyResourceData(amr.getStatus());
			return toResponse(bds,ReturnCode.SUCCESS,"查询成功");
		} catch (Exception e) {
			log.info("查询失败",e);
			return toResponse(null,ReturnCode.FAIL,"查询失败");
		}
		
	}
	
	/*********************************************************************************************************/
	@RequestMapping(value = "/queryAllBaseData1" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAllBaseData1(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try {
			BaseDataStatus bds = new BaseDataStatus();
			String pid=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==pid){
				return toResponse(bds,ReturnCode.NOLOGIN,"未登陆");
			}
			String subject=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			
			AuthSchoolRunningData asrd = authSchoolRunningDateService.queryAuthSchoolRunningDataBySchoolId(pid, subject);//办学数据
			AuthStudentData asd = authStudentDataService.queryStudentDataBySchoolId(pid, subject);
			AuthTeacherData atd = authTeacherDataService.queryAuthTeacherDataBySchoolId(pid, subject);
			AuthResourceData ard = authResourceDataService.queryAuthResourceDataBySchoolId(pid, subject);
			AuthTeachResource atr = authTeachResourceService.queryAuthTeachResourceDataBySchoolId(pid, subject);
			AuthMoneyResource amr = authMoneyResourceDataService.queryAuthMoneyResourcesBySchoolId(pid, subject);

				bds.setRunningData(asrd.getStatus());
				bds.setStudentData(asd.getStatus());
				bds.setTeacherData(atd.getStatus());
				bds.setResourceData(ard.getStatus());
				bds.setTeachResourceData(atr.getStatus());
				bds.setMomeyResourceData(amr.getStatus());
			return toResponse(bds,ReturnCode.SUCCESS,"查询成功");
		} catch (Exception e) {
			log.info("查询失败",e);
			return toResponse(null,ReturnCode.FAIL,"查询失败");
		}
		
	}
	
}
